#include "PWMManager.h"

PWMManager::PWMManager() {

	pwmarray = new int[MAX_PWMS];
	for (int i = 0; i < MAX_PWMS; ++i) {
		pwmarray[i] = (int)(new SafePWM(i + 1));
	}

}

SafePWM* PWMManager::GetPWM(int channel)
{
	// Don't read this... -Jason
	if (channel < 1 || channel > MAX_PWMS) {
		printf("GetPWM passed invalid channel: %d", channel);
		return &((SafePWM&)pwmarray[0]);
	}
	return &((SafePWM&)pwmarray[channel-1]);
	// Continue reading
}

PWMManager* PWMManager::Get() {
	if (!hasSingleton) {
		singleton = new PWMManager();
	}
	return singleton;
}

void PWMManager::SetPWM(int channel, float value) {
	if (channel < 1 || channel > MAX_PWMS) {
		printf("SetPWM passed invalid channel: %d", channel);
		return;
	}
	if (value < -1.0) {
		value = -1.0;
	}
	if (value > 1.0) {
		value = 1.0;
	}
	// Easy Way
	//value = (value < -1.0) ? -1.0 : value;
	//value = (value > 1.0) ? 1.0 : value;

	GetPWM(channel)->SetSpeed(value);

}

void PWMManager::StopPWM(int channel) {
	if (channel < 1 || channel > MAX_PWMS) {
		printf("StopPWM passed invalid channel: %d", channel);
		return;
	}
	GetPWM(channel)->StopMotor();
}

void PWMManager::StopAllPWM() {
	for (int i = 1; i <= MAX_PWMS; ++i) {
		GetPWM(i)->StopMotor();
	}
}


